<?php

/**
 * Description of YunPianSMS
 *
 * @author pc
 * @version 2017-12-17
 */
require_once APP_ROOT_PATH . "system/libs/sms.php";  //引入接口

class YunPian_sms implements sms {

    public function sendSMS($mobile_number, $content, $is_adv = 0) {
        header("Content-Type:text/html;charset=utf-8");
        $apikey = "f7ce5638514f2ca8489c2e9e2f42777a"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
        $mobile = is_array($mobile_number) ? implode(',', $mobile_number) : $mobile_number; //请用自己的手机号代替
        $text = $content;
        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间 */
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// 发送模板短信
// 需要对value进行编码 ('#code#'=' . urlencode('1234'))
        $data = array('tpl_id' => '2112096', 'tpl_value' => $content, 'apikey' => $apikey, 'mobile' => $mobile);
        $json_data = $this->tpl_send($ch, $data);
        curl_close($ch);
        $result = json_decode($json_data, true);
        if ($result) {
            $error_code = $result['code'];
            // array ( 'http_status_code' => 400, 'code' => 7, 'msg' => '模板不可用', 'detail' => '模板 2103722 可能在审核中或者审核不通过', )
            if ($error_code == 0) {
                $data['status'] = 1;
                $data['msg'] = "短信发送成功,短信ID：" . $result['sid'];
            } else {
                $data['status'] = 0;
                $data['msg'] = "短信发送失败(" . $error_code . ")：" . $result['msg'];
            }
        } else {
            //返回内容异常，以下可根据业务逻辑自行修改
            $data['status'] = 0;
            $data['msg'] = "请求发送短信失败";
        }
        if (intval($_REQUEST['debug']) > 0) {
            var_export($data);
        }
        return $data;
    }

    public function getSmsInfo() {

        return "云片网短信平台";
    }

    public function check_fee() {
        return '...';
    }

    /*     * ********************************************************************************* */

//获得账户
    function get_user($ch, $apikey) {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function send($ch, $data) {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function tpl_send($ch, $data) {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function batch_send($ch, $data) {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/batch_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function voice_send($ch, $data) {
        curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function notify_send($ch, $data) {
        curl_setopt($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function checkErr($result, $error) {
        if ($result === false) {
            echo 'Curl error: ' . $error;
        } else {
            //echo '操作完成没有任何错误';
        }
    }

}
